package com.tfswx.jkgl.core.sjcc.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.tfswx.jkgl.core.dao.SqlMapper;
import com.tfswx.jkgl.core.sjcc.ScclBase;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Smile
 * @date 2021/12/14
 */
@Component
public class Dwbm implements ScclBase {

    @Override
    public Object getValue(Object param) {
        try {
            String res = "";
            JSONObject obj = (JSONObject) param;
            if (obj == null) {
                throw new RuntimeException("策略参数属性不能为空");
            }
            if (obj.getOrDefault("DWJB", null) == null) {
                throw new RuntimeException("单位级别不能为空");
            }
            String data = null;
            try (SqlMapper sqlMapper = new SqlMapper()) {
                Object o = sqlMapper.selectOne("select max(dwbm)+1 DWBM from T_ZZJG_XT_DWXX where dwjb = #{DWJB}", obj, null);
                if (o != null) {
                    data = String.valueOf(((Map<String, Long>) o).get("DWBM"));
                }
            }
            // String data = sjczMapper.selectString("select max(dwbm)+1 dwbm from T_ZZJG_XT_DWXX where dwjb = " + obj.getString("DWJB"));

            if (StrUtil.isBlank(data)) {
                res = StrUtil.padAfter(obj.getString("DWJB"), 4, "0") + StrUtil.padPre("1", 6, "0");
            } else {
                res = data;
            }
            return res;
        } catch (RuntimeException e) {
            throw new RuntimeException("执行生成策略失败：" + e.getMessage());
        }
    }

    @Override
    public String getName() {
        return "单位编码";
    }

    @Override
    public boolean sfxycs() {
        return true;
    }
}
